#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
数据库表结构检查脚本
用于检查数据库表的实际结构
"""

import sqlite3
import os

def check_database_structure():
    """检查数据库表结构"""
    # 检查所有可能的数据库文件位置
    db_paths = [
        'vulnerability_scanner.db',
        'vulnerability.db',
        'instance/vulnerability_scanner.db',
        'instance/vulnerability.db'
    ]
    
    for db_path in db_paths:
        if os.path.exists(db_path):
            print(f"\n找到数据库文件: {db_path}")
            try:
                # 连接数据库
                conn = sqlite3.connect(db_path)
                cursor = conn.cursor()
                
                # 获取所有表名
                cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
                tables = cursor.fetchall()
                
                print(f"数据库中的表: {[table[0] for table in tables]}")
                
                # 检查scans表的结构
                if 'scans' in [table[0] for table in tables]:
                    print("\nscans表的列:")
                    cursor.execute("PRAGMA table_info(scans);")
                    columns = cursor.fetchall()
                    for column in columns:
                        print(f"  {column[1]} - {column[2]}")
                else:
                    print("\nscans表不存在！")
                
                # 关闭连接
                conn.close()
                
            except Exception as e:
                print(f"检查数据库时出错: {e}")
    
    if not any(os.path.exists(path) for path in db_paths):
        print("未找到任何数据库文件！")

if __name__ == '__main__':
    check_database_structure()